#include "semAndQueue_demo.h"
UINT16 semCount1 = 2;
extern TSK_INIT_PARAM_S task1;
extern TSK_INIT_PARAM_S task2;
char* arr[100];

static VOID TaskF01(void)
{
    UINT32 ret;
    //printf("\n+++++++++++++++++++++++TaskF01:(1) before sem pend success,check actual Count = %d,it should be 2\n",Count);
    //ret = LOS_SemPend(g_semTest1,LOS_WAIT_FOREVER);
    //Count++;
    //LOS_TaskDelay(50);
    printf("\n++++++++++++task1接受到来自task2的消息，消息为：%s 并释放一个信号量，信号量为：%d\n",arr[0],semCount1);
    ret = LOS_SemPost(g_semTest2);
    //printf("\n+++++++++++++++++++++++TaskF01:(2) before sem post success, check actual Count = %d,it should be 3\n",Count);
    return;
}

static VOID TaskF02(void)
{
    UINT32 ret;

    //Count++;
    //printf("\n+++++++++++++++++++++++TaskF02:(1) before sem pend success, check actual Count = %d,it should be 1\n",Count);
    arr[0] = "HI task1";
    printf("\n++++++++++++++++task2申请信号量并向字符串数组里写入消息: %s ，此时信号量为%d\n",arr[0],semCount1);
    ret = LOS_SemPend(g_semTest2,LOS_WAIT_FOREVER);
    printf("\n+++++++++task2让出50ticks的时间\n");
    
    // Count++;
    // printf("\n+++++++++++++++++++++++TaskF02:(2) after sem pend success, check actual Count = %d,it should be 2\n",Count);
    // printf("\n+++++++++++++++++++++++TaskF02:(3) continue hold sem for 50 ticks,TaskF01 should be scheduled\n");

    LOS_TaskDelay(50);
    printf("\n+++++++++++++++++++++++TaskF02:(4) sem post, TaskF01 will continue exe within the next 50 ticks\n");
    //ret = LOS_SemPost(g_semTest1);
    LOS_TaskDelay(10);

    //Count++;
    //printf("\n+++++++++++++++++++++++TaskF02:(5) after sem post check actual Count = %d,it should be 4\n",Count);
}

VOID  Sem_Demo1(VOID)
{
    UINT32 ret;

    ret = LOS_SemCreate(semCount1,&g_semTest2);
    if(ret==LOS_OK){
        printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~This is liweili 扩展作业~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
        printf("\n+++++++++++++++++++利用信号量初始值为2创建长度为：%d的队列\n",semCount1);
    }

    LOS_TaskLock();

    task1.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskF01;
    task1.usTaskPrio = TASK_PRIO_TEST;
    task1.pcName = "VSemB2_1";
    task1.uwStackSize = 0x900;
    task1.uwResved = 0;

    ret = LOS_TaskCreate(&g_testTaskID01,&task1);
    if(ret==LOS_OK){
        printf("\n+++++++++++++++++++SemDemo:TaskF01 created ok with ID = 0x%x\n",g_testTaskID01);
    }

    task2.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskF02;
    task2.usTaskPrio = (TASK_PRIO_TEST-2);
    task2.pcName = "VSemB2_@";
    task2.uwStackSize = 0x900;
    task2.uwResved = 0;

    ret = LOS_TaskCreate(&g_testTaskID02,&task2);
    if(ret==LOS_OK){
        printf("\n+++++++++++++++++++SemDemo:TaskF02 created ok with ID = 0x%x\n",g_testTaskID02);
    }

    LOS_TaskUnlock();

    printf("\n++++++++++++++++++++Sem Demo:will delay 92 ticks\n");
    LOS_TaskDelay(90);

    //printf("\n++++++++++++++++++++Sem Demo:after 90ticks, finally count = %d\n",Count);

    ret = LOS_SemDelete(g_semTest2);
    if(ret==LOS_OK){
        printf("\n++++++++++++++++++++Sem Demo:g_semTest2 deleted ok\n");
    }
}

